package com.fsoft.manager.dept.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fsoft.core.Global;
import com.fsoft.core.annotation.SystemLog;
import com.fsoft.core.common.QueryParam;
import com.fsoft.core.common.base.contorller.BaseController;
import com.fsoft.core.utils.DateTimeUtils;
import com.fsoft.core.utils.OgnlUtils;
import com.fsoft.core.utils.RetVo;
import com.fsoft.core.utils.tree.BuildTree;
import com.fsoft.manager.dept.entity.SysDept;
import com.fsoft.manager.dept.service.SysDeptService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * F-Soft 部门基本信息管理
 * 
 * @package com.fsoft.manager.dept.controller
 * @author Fish
 * @email it.fish2010@foxmail.com
 * @date 2021-03-31
 * @CopyRight © F-Soft
 **/
@Controller
@RequestMapping("/sys-dept")
public class SysDeptController extends BaseController {
	@Autowired
	private SysDeptService service;

	@ResponseBody
	@RequestMapping(value = "/findList", method = RequestMethod.POST)
	public RetVo findList(@RequestParam Map<String, Object> params) throws Exception {
		try {
			QueryParam query = new QueryParam(params);
			Page<SysDept> page = PageHelper.startPage(query.getPage(), query.getLimit());
			List<SysDept> list = service.findList(query);
			return RetVo.ok(page.getTotal(), list);
		} catch (Exception e) {
			return RetVo.error(e.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping(value = "/findTrees", method = RequestMethod.POST)
	public RetVo findTrees(@RequestParam Map<String, Object> params) throws Exception {
		QueryParam queryParam = new QueryParam(params);
		List<SysDept> trees = service.findList(queryParam);
		return RetVo.ok(trees.size(), BuildTree.buildTree(trees));
	}

	private String path = "/fsoft-web/dept/sysdept-";

	/***
	 * 页面跳转主入口，支持：<BR>
	 * page = sysdept_list: 部门管理主页面<BR>
	 * page = sysdept_modify: 部门编辑/部门新增<bR>
	 * page = sysdept_budget: 设置预算额度预警阈值
	 * 
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2021-03-31
	 * @param model
	 * @param page
	 * @param id
	 * @return
	 */
	@RequestMapping(value = { "/index/{page}", "/index/{page}/{id}" })
	public ModelAndView index(@PathVariable("page") String page, @PathVariable(value = "id", required = false) String id) throws Exception {
		if (StringUtils.isBlank(page))
			page = "list";
		ModelAndView mv = new ModelAndView(path + page);
		if (StringUtils.isNotBlank(id)) {
			SysDept dept = service.getEntity(id);
			if (dept != null)
				mv.addObject("model", dept);
		}
		return mv;
	}

	@ResponseBody
	@SystemLog("添加部门")
	@RequestMapping(value = "/save", method = RequestMethod.PUT)
	@RequiresPermissions("sys:dept:save")
	public RetVo save(@RequestBody SysDept area) throws Exception {
		try {
			// 设置排序
			if (OgnlUtils.isEmpty(area.getSortNo()))
				area.setSortNo(BigDecimal.ONE);
			area.setStatus(Global.STATUS_YES);
			area.setCreateUserId(getUserId());
			area.setCreateTime(DateTimeUtils.getNowTime());
			service.save(area);
			return RetVo.ok();
		} catch (Exception e) {
			return RetVo.error(e.getMessage());
		}

	}

	@ResponseBody
	@SystemLog("编辑部门")
	@RequestMapping(value = "/modify", method = RequestMethod.PUT)
	@RequiresPermissions("sys:dept:modify")
	public RetVo modify(@RequestBody SysDept area) throws Exception {
		try {
			area.setModifyUserId(getUserId());
			area.setModifyTime(DateTimeUtils.getNowTime());
			service.modify(area);
			return RetVo.ok();
		} catch (Exception e) {
			return RetVo.error(e.getMessage());
		}
	}

	/***
	 * F-Soft 删除部门
	 * 
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2021-03-31
	 * @param areaIds
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@SystemLog("删除部门")
	@RequestMapping("/remove")
	@RequiresPermissions("sys:dept:remove")
	public RetVo remove(@RequestBody String[] areaIds) throws Exception {
		service.removeBatch(Arrays.asList(areaIds));
		return RetVo.ok();
	}
}
